﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MigratorCore.Bases;
using MSDASC;
using ADODB;
using System.Reflection;

namespace MigratorWizard
{
    public partial class SettingsDialog : Form
    {
        private Settings settings;
        public SettingsDialog(Settings settings, string of)
        {
            this.settings = settings;
            InitializeComponent();
            this.Text = string.Format(this.Text, of);
            Title.Text = string.Format(Title.Text, of);
            Description.Text = string.Format(Description.Text, of);
            parseSettings();
        }

        private void parseSettings()
        {
            for (int x = 0; x < settings.Count; x++)
            {
                RowStyle rs = new RowStyle(controlTable.RowStyles[0].SizeType, controlTable.RowStyles[0].Height);
                controlTable.RowStyles.Add(rs);
                parseSetting(settings.Keys[x], settings[x], x);
            }
            controlTable.RowStyles.RemoveAt(controlTable.RowCount - 1);
        }

        private void parseSetting(string name, object value, int row)
        {
            if (value.GetType() == typeof(bool))
            {
                CheckBox cb = new CheckBox();
                cb.Checked = (bool)value;
                cb.Text = "Yes/No";
                cb.Name = name.ToLower().Replace(" ", "").Trim();
                Label lb = new Label();
                lb.Height = 25;
                lb.Dock = DockStyle.Top;
                lb.TextAlign = ContentAlignment.BottomRight;
                lb.AutoSize = false;
                lb.Text = name + ": ";
                controlTable.Controls.Add(lb, 0, row);
                controlTable.Controls.Add(cb, 1, row);
                return;
            }
            else if (value.GetType() == typeof(DateTime))
            {
                DateTimePicker dt = new DateTimePicker();
                dt.Value = (DateTime)value;
                dt.Name = name.ToLower().Replace(" ", "").Trim();
                Label lb = new Label();
                lb.Height = 25;
                lb.Dock = DockStyle.Top;
                lb.TextAlign = ContentAlignment.BottomRight;
                lb.AutoSize = false;
                lb.Text = name + ": ";
                controlTable.Controls.Add(lb, 0, row);
                controlTable.Controls.Add(dt, 1, row);
                return;
            }
            else if (value.GetType() == typeof(int))
            {
                NumericUpDown nu = new NumericUpDown();
                nu.Value = (int)value;
                nu.Name = name.ToLower().Replace(" ", "").Trim();
                Label lb = new Label();
                lb.Height = 25;
                lb.Dock = DockStyle.Top;
                lb.TextAlign = ContentAlignment.BottomRight;
                lb.AutoSize = false;
                lb.Text = name + ": ";
                controlTable.Controls.Add(lb, 0, row);
                controlTable.Controls.Add(nu, 1, row);
                return;
            }
            else if (value.GetType().IsEnum)
            {
                VistaControls.ComboBox cb = new VistaControls.ComboBox();
                cb.Name = name.ToLower().Replace(" ", "").Trim();
                foreach (string s in Enum.GetNames(value.GetType()))
                    if (s != "value__")
                        cb.Items.Add(s);
                cb.SelectedItem = value.ToString();
                Label lb = new Label();
                lb.Height = 25;
                lb.Dock = DockStyle.Top;
                lb.TextAlign = ContentAlignment.BottomRight;
                lb.AutoSize = false;
                lb.Text = name + ": ";
                controlTable.Controls.Add(lb, 0, row);
                controlTable.Controls.Add(cb, 1, row);
                return;
            }
            else
            {
                TextBox tb = new TextBox();
                tb.Text = value.ToString();
                tb.Name = name.Replace(" ", "").Trim();
                tb.Width = 290;
                Label lb = new Label();
                lb.Height = 25;
                lb.Dock = DockStyle.Top;
                lb.TextAlign = ContentAlignment.BottomRight;
                lb.AutoSize = false;
                lb.Text = name + ": ";
                controlTable.Controls.Add(lb, 0, row);
                if (name.Replace(" ", "").ToLower().Trim().Equals("connectionstring")) // Add a button for the builder
                {
                    VistaControls.Button button = new VistaControls.Button();
                    button.Text = "...";
                    button.Click += new EventHandler(button_Click);
                    button.Name = "btn" + name.Replace(" ", "").Trim();
                    button.Width = 20;
                    FlowLayoutPanel flp = new FlowLayoutPanel();
                    flp.Height = 25;
                    flp.FlowDirection = FlowDirection.LeftToRight;
                    flp.Dock = DockStyle.Top;
                    flp.Controls.Add(tb);
                    flp.Controls.Add(button);
                    controlTable.Controls.Add(flp, 1, row);
                }
                else controlTable.Controls.Add(tb, 1, row);
            }
        }

        void button_Click(object sender, EventArgs e)
        {
            VistaControls.Button btn = sender as VistaControls.Button;
            TextBox tb = controlTable.Controls.Find(btn.Name.Replace("btn", ""), true)[0] as TextBox;
            MSDASC.DataLinks mydlg = new MSDASC.DataLinks();
            ADODB._Connection ADOcon;

            //Cast the generic object that PromptNew returns to an ADODB._Connection.
            ADOcon = (ADODB._Connection)mydlg.PromptNew();

            try
            {
                ADOcon.Open("", "", "", 0);

                if (ADOcon.State == 1)
                {
                    MessageBox.Show("Connection Good!");
                    tb.Text = ADOcon.ConnectionString;
                    ADOcon.Close();
                }
                else
                {
                    MessageBox.Show("Connection Failed");
                }
            }
            catch { }
        }

        public static T StringToEnum<T>(string name)
        {
            return (T)Enum.Parse(typeof(T), name);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            for (int x = 0; x < settings.Count; x++)
            {
                if (settings[x].GetType() == typeof(bool))
                {
                    CheckBox cl = controlTable.Controls.Find(settings.Keys[x].ToLower().Replace(" ", "").Trim(), true)[0] as CheckBox;
                    settings[x] = cl.Checked;
                }
                else if (settings[x].GetType() == typeof(DateTime))
                {
                    DateTimePicker dt = controlTable.Controls.Find(settings.Keys[x].ToLower().Replace(" ", "").Trim(), true)[0] as DateTimePicker;
                    settings[x] = dt.Value;
                }
                else if (settings[x].GetType() == typeof(int))
                {
                    NumericUpDown nu = controlTable.Controls.Find(settings.Keys[x].ToLower().Replace(" ", "").Trim(), true)[0] as NumericUpDown;
                    settings[x] = nu.Value;
                }
                else if (settings[x].GetType().IsEnum)
                {
                    ComboBox cb = controlTable.Controls.Find(settings.Keys[x].ToLower().Replace(" ", "").Trim(), true)[0] as ComboBox;
                    settings[x] = Enum.Parse(settings[x].GetType(), cb.SelectedItem.ToString());
                }
                else
                {
                    TextBox tb = controlTable.Controls.Find(settings.Keys[x].ToLower().Replace(" ", "").Trim(), true)[0] as TextBox;
                    settings[x] = tb.Text;
                }
            }
        }

        private void bottomPanel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.FromArgb(223, 223, 223))), 0, 0, bottomPanel.Width, 0);
        }
    }
}
